/*
 * 版权所有 (c) 2022-2099 TBIT。保留所有权利。
 */


package com.tbit.main.sms.client.impl;

import com.tbit.main.sms.client.SmsClient;
import com.tbit.main.sms.client.SmsClientFactory;
import com.tbit.main.sms.enums.SmsChannelEnum;
import com.tbit.main.sms.properties.SmsChannelProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;

/**
 * @author <a href="mailto:li.yao@tbit.com.cn">yao</a>
 * @version 4.1.0
 * @since 2024/7/12 11:00
 */
@Slf4j
@Validated
@Component
public class SmsClientFactoryImpl implements SmsClientFactory {

    @Override
    public SmsClient getSmsClient(SmsChannelProperties properties) {
        return createSmsClient(properties);
    }

    private AbstractSmsClient createSmsClient(SmsChannelProperties properties) {
        SmsChannelEnum channelEnum = SmsChannelEnum.getByCode(properties.getCode());
        Assert.notNull(channelEnum, String.format("渠道类型(%s) 为空", channelEnum));
        // 创建客户端
        switch (channelEnum) {
            case UQ: return new UQSmsClient(properties);
            case CHUANG_LAN: return new ChuangLanSmsClient(properties);
            case API_STORE: return new ApiStoreSmsClient(properties);
        }
        // 创建失败，错误日志 + 抛出异常
        log.error("[createSmsClient][配置 找不到合适的客户端实现]");
        throw new IllegalArgumentException(String.format("配置(%s) 找不到合适的客户端实现", properties));
    }
}
